package cn.yuanqiao.archive.common.typeHandler;

import cn.yuanqiao.common.config.YqDatabaseIdProvider;
import cn.yuanqiao.common.enums.DbIdEnum;
import com.alibaba.druid.proxy.jdbc.NClobProxyImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.type.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.*;

/**
 *
 *@AUTHOR:赵常飞
 *@date 2023/10/10 16:50
 */
@MappedTypes(Clob.class)
@MappedJdbcTypes(JdbcType.CLOB)
@Component
public class NClobTypeHandler extends BaseTypeHandler<String> {

    @Autowired
    private DataSource dataSource;
    @Resource
    private YqDatabaseIdProvider yqDatabaseIdProvider;

    private static String dataBaseId;

    @PostConstruct
    public void getDatabaseId() throws SQLException {
        dataBaseId = yqDatabaseIdProvider.getDatabaseId(dataSource);
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        StringReader reader=new StringReader(parameter);
        ps.setCharacterStream(i,reader,parameter.length());
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) {
        String clobToString="";
        try {
            if(DbIdEnum.DM.getCode().equalsIgnoreCase(dataBaseId)){
                NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(columnName);
                if (aa!=null) {
                    Clob rawClob = aa.getRawClob();
                    clobToString = ClobToString(rawClob);
                }
            }else{
                clobToString="长文本类型,无法显示";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clobToString;
    }

    @Override
    public String getNullableResult(ResultSet rs, int i) {
        String clobToString="";
        try {
            if(DbIdEnum.DM.getCode().equalsIgnoreCase(dataBaseId)){
                NClobProxyImpl aa=(NClobProxyImpl) rs.getObject(i);
                Clob rawClob = aa.getRawClob();
                clobToString = ClobToString(rawClob);
            }else{
                clobToString="长文本类型,无法显示";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clobToString;
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i){
        String clobToString="";
        try {
            if(DbIdEnum.DM.getCode().equalsIgnoreCase(dataBaseId)){
                NClobProxyImpl aa=(NClobProxyImpl) callableStatement.getObject(i);
                Clob rawClob = aa.getRawClob();
                clobToString = ClobToString(rawClob);
            }else{
                clobToString="长文本类型,无法显示";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clobToString;
    }


    private String ClobToString(Clob clob) throws SQLException, IOException {
        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
}
